define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'merchant/index' + location.search,
                    add_url: 'merchant/add',
                    edit_url: 'merchant/edit',
                    del_url: 'merchant/del',
                    multi_url: 'merchant/multi',
                    import_url: 'merchant/import',
                    table: 'merchant',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'merchant_id',
                sortName: 'merchant_id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'merchant_id', title: "ID"},
                        {field: 'merchant_name', title: "企业名称", operate: 'LIKE'},
                        {field: 'abbr', title: "企业简称", operate: 'LIKE'},
                        // {field: 'merchant_image', title: __('Merchant_image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'merchant_tel', title: __('Merchant_tel'), operate: 'LIKE'},
                        {field: 'address', title: __('Address'), operate: 'LIKE'},
                        {field: 'weigh', title: "排序", operate: false},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();

            $(".btn-add-merchant").on("click",function(){
				var param = $("#add-form").serializeArray();
				Fast.api.ajax({
					url:"merchant/addMerchant",
                    data:param,
                    success: function(data){
                        if (data.code == 1) {
                            // console.log(data);
                            var content = "企业后台账号：" + data.msg.telephone + "<br/>" + "默认密码：" + data.msg.telephone;
                            layer.open({
                                title:"添加企业成功",
                                content: content,
                                // shadeClose:true,
                                yes:function (index,layero) {
                                    parent.Layer.closeAll();
                                    parent.location.reload();
                                }
                            });
                        } else {
                            Toastr.error(data.msg);
                            Layer.closeAll();
                        }
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        Toastr.error("网络错误，请稍后重试！");
                        console.log('提交请求的错误信息：' + errorThrown + XMLHttpRequest);
                    }
				});
			});
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        },
    };
    return Controller;
});